package com.lxmes.business.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.lxmes.base.domain.JcWarehouseinfoKuwei;
import com.lxmes.base.mapper.JcWarehouseinfoKuweiMapper;
import com.lxmes.business.domain.*;
import com.lxmes.business.mapper.CcRukuDetailMapper;
import com.lxmes.business.mapper.CcStockinfoInfoMapper;
import com.lxmes.business.mapper.CcWarehouserecordMapper;
import com.lxmes.common.annotation.DataScope;
import com.lxmes.common.exception.ServiceException;
import com.lxmes.common.utils.DateUtils;
import com.lxmes.common.utils.NewNumber;
import com.lxmes.common.utils.SecurityUtils;
import com.lxmes.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.business.mapper.CcRukuMainMapper;
import com.lxmes.business.service.ICcRukuMainService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 入库任务Service业务层处理
 * 
 * @author 张志煜
 * @date 2022-07-03
 */
@Service
public class CcRukuMainServiceImpl implements ICcRukuMainService 
{
    @Autowired
    private CcRukuMainMapper ccRukuMainMapper;
    @Autowired
    private CcRukuDetailMapper ccRukuDetailMapper;
    @Autowired
    private CcStockinfoInfoMapper stockinfoInfoMapper;
    @Autowired
    private CcWarehouserecordMapper warehouserecordMapper;
    @Autowired
    private JcWarehouseinfoKuweiMapper kuweiMapper;


    /**
     * 查询入库任务
     * 
     * @param id 入库任务主键
     * @return 入库任务
     */
    @Override
    public CcRukuMain selectCcRukuMainById(Long id)
    {
        return ccRukuMainMapper.selectCcRukuMainById(id);
    }

    @Override
    public CcRukuMain selectCcRukuMainByDanHao(String danhao) {
        return ccRukuMainMapper.selectCcRukuMainByDanHao(danhao);
    }

    @Override
    public List<CcRukuMain> selectCcRukuMainByRukuFlag(String rukuFlag) {
        return ccRukuMainMapper.selectCcRukuMainByRukuFlag(rukuFlag);
    }

    /**
     * 查询入库任务列表
     * 
     * @param ccRukuMain 入库任务
     * @return 入库任务
     */
    @Override
    @DataScope(deptAlias = "de", userAlias = "de")
    public List<CcRukuMain> selectCcRukuMainList(CcRukuMain ccRukuMain)
    {
        return ccRukuMainMapper.selectCcRukuMainList(ccRukuMain);
    }

    /**
     * 新增入库任务
     * 
     * @param ccRukuMain 入库任务
     * @return 结果
     */
    @Override
    @Transactional
    public int insertCcRukuMain(CcRukuMain ccRukuMain)
    {
        ccRukuMain.setCreateTime(DateUtils.getNowDate());
        ccRukuMain.setCreateBy(SecurityUtils.getUsername());
        ccRukuMain.setDeptId(SecurityUtils.getDeptId());
        ccRukuMain.setDeptId(SecurityUtils.getDeptId());
        String number= "RK-"+ NewNumber.createNewNumber(ccRukuMainMapper.getNewCode(DateUtils.getDate()));
        ccRukuMain.setDanhao(number);
        List<CcRukuDetail> list = ccRukuMain.getInfoList();
        if(list.size()>0){
            for ( CcRukuDetail info:list) {
                //JcWarehouseinfoKuwei kuwei = kuweiMapper.selectJcWarehouseinfoKuweiByKuweiJmCode(info.getKuweiCode());
                info.setDanhao(number);
                info.setRukuDate(ccRukuMain.getRukuDate());
                info.setStockName(ccRukuMain.getStockName());
                info.setStockId(ccRukuMain.getStockId());
               /* info.setKuquName(kuwei.getKuquName());
                info.setKuquCode(kuwei.getKuquCode());
                info.setKuweiCode(kuwei.getKuweiJmCode());*/
                info.setCreateTime(DateUtils.getNowDate());
                info.setCreateBy(SecurityUtils.getUsername());
                ccRukuDetailMapper.insertCcRukuDetail(info);
            }
        }else {
            throw new ServiceException("物料不存在！");
        }
        ccRukuMain.setLot(DateUtils.dateTime());
        Map<String, String> map = ccRukuDetailMapper.tJRukuDetail(number);
        ccRukuMain.setTotalNum(new BigDecimal(String.valueOf(map.get("rkNum"))));
        ccRukuMain.setTotalPrice(new BigDecimal(String.valueOf(map.get("zPrice"))));
       // ccRukuMain.setJzFlag("未结清");
        //ccRukuMain.setZfTotalPrice(new BigDecimal(String.valueOf(map.get("zPrice"))).subtract(ccRukuMain.getYhTotalPrice()));
        ccRukuMainMapper.insertCcRukuMain(ccRukuMain);
        return 1;
    }

    @Override
    @Transactional
    public int zhixingRuku(String danhao) {
        CcRukuMain main = ccRukuMainMapper.selectCcRukuMainByDanHao(danhao);
        List<CcRukuDetail> detailList = ccRukuDetailMapper.selectCcRukuDetailByDanHao(danhao);
        if(detailList.size()>0){
            //遍历所有带入库的物料
            for (CcRukuDetail detail:detailList) {
                //查询库存的物料
                 CcStockinfoInfo stockInfo = stockinfoInfoMapper.selectCcStockinfoInfoByItemIdAndLot(detail.getItemId(),detail.getLot());
                 //如果是null新增物料库存 否则修改新增库存
                 if(stockInfo==null){
                     stockInfo =new CcStockinfoInfo();
                     stockInfo.setDeptId(SecurityUtils.getDeptId());
                     stockInfo.setItemId(detail.getItemId());
                     stockInfo.setItemCode(detail.getItemId());
                     stockInfo.setItemName(detail.getItemName());
                     stockInfo.setSpec(detail.getSpec());
                     stockInfo.setUnit(detail.getUnit());
                     stockInfo.setKcNum(detail.getRukuNum());
                     stockInfo.setQutlityStauts(0L);
                     stockInfo.setSupplyId(detail.getSupplyId());
                     stockInfo.setSupplyName(detail.getSupplyName());
                     stockInfo.setClassId(detail.getClassId());
                     stockInfo.setClassName(detail.getClassName());
                     stockInfo.setStockId(main.getStockId());
                     stockInfo.setStockName(main.getStockName());
                     stockInfo.setKuquCode(detail.getKuquCode());
                     stockInfo.setKuquName(detail.getKuquName());
                     stockInfo.setKuweiCode(detail.getKuweiCode());
                     stockInfo.setCreateBy(SecurityUtils.getUsername());
                     stockInfo.setCreateTime(new Date());
                     stockInfo.setLot(detail.getLot());
                     stockInfo.setUnitPrice(detail.getUnitPrice());
                     stockinfoInfoMapper.insertCcStockinfoInfo(stockInfo);
                 }else {
                     stockInfo.setUpdateBy(SecurityUtils.getUsername());
                     stockInfo.setUpdateTime(new Date());
                     stockInfo.setKcNum(stockInfo.getKcNum().add(detail.getRukuNum()));
                     stockinfoInfoMapper.updateCcStockinfoInfo(stockInfo);
                 }
                 //新增入库流水
                CcWarehouserecord warehouserecord=new CcWarehouserecord();
                warehouserecord.setDeptId(SecurityUtils.getDeptId());
                warehouserecord.setItemId(detail.getItemId());
                warehouserecord.setItemName(detail.getItemName());
                warehouserecord.setBsinssType(1L);
                warehouserecord.setCrNum(detail.getRukuNum());
                warehouserecord.setSupplyId(detail.getSupplyId());
                warehouserecord.setSupplyName(detail.getSupplyName());
                warehouserecord.setSpec(detail.getSpec());
                warehouserecord.setUnit(detail.getUnit());
                warehouserecord.setClassId(detail.getClassId());
                warehouserecord.setClassName(detail.getClassName());
                warehouserecord.setCreateBy(SecurityUtils.getUsername());
                warehouserecord.setCreateTime(new Date());
                warehouserecordMapper.insertCcWarehouserecord(warehouserecord);
            }
            main.setState(1L);
            ccRukuMainMapper.updateCcRukuMain(main);
        }else {
            throw new ServiceException("入库单物料不存在！");
        }
        return 1;
    }

    /**
     * 修改入库任务
     * 
     * @param ccRukuMain 入库任务
     * @return 结果
     */
    @Override
    @Transactional
    public int updateCcRukuMain(CcRukuMain ccRukuMain)
    {
        CcRukuDetail info=new CcRukuDetail();
        info.setDanhao(ccRukuMain.getDanhao());
        List<CcRukuDetail> oldInfoList = ccRukuDetailMapper.selectCcRukuDetailList(info);
        for (CcRukuDetail oldInfo:oldInfoList){
            ccRukuDetailMapper.deleteCcRukuDetailById(oldInfo.getId());
        }
        List<CcRukuDetail> newInfoList = ccRukuMain.getInfoList();
        for (CcRukuDetail newInfo:newInfoList){
            newInfo.setCreateTime(DateUtils.getNowDate());
            newInfo.setCreateBy(SecurityUtils.getUsername());
            info.setStockName(ccRukuMain.getStockName());
            info.setStockId(ccRukuMain.getStockId());
            newInfo.setDanhao(ccRukuMain.getDanhao());
            ccRukuDetailMapper.insertCcRukuDetail(newInfo);
        }
        Map<String, String> map = ccRukuDetailMapper.tJRukuDetail(ccRukuMain.getDanhao());
        ccRukuMain.setTotalNum(new BigDecimal(String.valueOf(map.get("rkNum"))));
        ccRukuMain.setTotalPrice(new BigDecimal(String.valueOf(map.get("zPrice"))));
        //ccRukuMain.setZfTotalPrice(new BigDecimal(String.valueOf(map.get("zPrice"))).subtract(ccRukuMain.getYhTotalPrice()));
        ccRukuMainMapper.updateCcRukuMain(ccRukuMain);
        return 1;
    }

    /**
     * 批量删除入库任务
     * 
     * @param ids 需要删除的入库任务主键
     * @return 结果
     */
    @Override
    public int deleteCcRukuMainByIds(Long[] ids)
    {
        return ccRukuMainMapper.deleteCcRukuMainByIds(ids);
    }

    /**
     * 删除入库任务信息
     * 
     * @param id 入库任务主键
     * @return 结果
     */
    @Override
    public int deleteCcRukuMainById(Long id)
    {
        return ccRukuMainMapper.deleteCcRukuMainById(id);
    }

    @Override
    @Transactional
    public int deleteCcRukuMainByDanhao(String danhao) {
        ccRukuMainMapper.deleteCcRukuMainByDanhao(danhao);
        ccRukuDetailMapper.deleteCcRukuDetailByDanhao(danhao);
        return 1;
    }
}
